<template>
  <div>
    <van-nav-bar title="京东登录" left-text="返回" left-arrow @click-left="$router.go(-1)" />
    <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认" @add="onAdd" @edit="onEdit" />
    <van-action-sheet v-model:show="show" title="name">
      <div class=" content">
        <van-address-edit :area-list="areaList" :address-info="addressinfo" show-postal show-delete show-set-default
          show-search-result :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']"
          @save="onSave" @delete="onDelete" @change-detail="onChangeDetail" />
      </div>
    </van-action-sheet>
  </div>
</template>

<script setup lang='ts'>
import { onMounted, ref, type Ref } from 'vue';
import { Toast } from 'vant';
import { areaList } from '@vant/area-data';
import { tjdz } from '@/api/tjdz'
import { dzlb } from '@/api/dzlb'
import { useUserStore } from '@/store/userstore';
import { useRouter } from 'vue-router';
import { scdz } from '@/api/scdz';
// import { useUserStore } from '@/store/userstore';
// const userstore=useUserStore()
const router = useRouter()
const useStore = useUserStore();
const chosenAddressId = ref('1');
const show = ref(false)
const searchResult = ref([]);
// type miaoshaArrr = {
//   postalCode: object,
//   [propName: string]: any
// } Ref<Array<miaoshaArrr>> 
let obj: any = ref([])
let list: any = ref([]);
// console.log('asdsadsadsa', list.value);

const userid: any = localStorage.getItem('userid')


// 关于邮箱的整活没整好
// const postalCode1: any = ref('')
// postalCode1.value = useStore.userId
// console.log(3242432423432423, postalCode1.value);

// 列表地址
onMounted(() => {
  dzlb({ userid: userid }).then((res: any) => {
    console.log(456789, res);
    list.value = res.data
    console.log("地址的信息", list.value);
    list.value.forEach((item: any, index: any) => {
      // console.log(index);
      // item.id = index
      item.address = item.province + item.county + item.addressDetail
    });
    // console.log('list里面的东西', list.value);
  })
})
// 点击新增地址弹出模态框


const onAdd = () => { show.value = true };
// 点击模态框里面的保存
const onSave = (content: any) => {
  router.go(0)
  obj = {
    userid: userid,
    name: content.name,
    tel: content.tel,
    province: content.province,
    city: content.city,
    county: content.county,
    postalCode: content.postalCode,
    addressDetail: content.addressDetail,
    isDefault: content.isDefault,
  }
  console.log('阿山东黄金卡萨的', obj);
  useStore.userId += obj.postalCode
  // 添加地址
  tjdz(obj).then((res: any) => {

    if (res.code == '200') {
      show.value = false
      list.value.push(obj)
      console.log('添加地址', list.value);

    }
  })

};
// 删除地址的
let dzid: any = ref([])
const onDelete = (content: any) => {
  router.go(0)
  console.log('asdsadadsadasdadadsasad', content);

  // dzid = { addressid: content.addressid }
  scdz({ addressid: content.addressid }).then(res => {
    console.log('删除了', res);
    show.value = false

  })
};
const onChangeDetail = (val: any) => {
  if (val) {
    searchResult.value = [

    ];
  } else {
    searchResult.value = [];
  }
};

const disabledList = [
  {
    id: '3',
    name: '王五',
    tel: '1320000000',
    address: '浙江省杭州市滨江区江南大道 15 号',
  },
];
// 地址信息初始值 （就是刚开始的值）
const addressinfo = ref({})


// 编辑回填功能
const onEdit = (item: any, index: any) => {

  console.log('会填的item', item);
  // item.push()    
  // 弹出模态框
  show.value = true
  // 往初始值里面写东西
  addressinfo.value = {
    id: item.userid,
    name: item.name,
    tel: item.tel,
    addressDetail: item.address,
    province: item.province,
    postalCode: useStore.userId.slice(0, 6),
    city: item.city,
    county: item.county,
    address: item.address,
    addressid: item.addressid,
    isDefault: item.isDefault,
    areaCode: getareaCode(item.province, item.city, item.county)
  }
};
function getareaCode(province: string, city: string, county: string) {
  let areaCode = ''
  let provinceObj: { [name: string]: any } = areaList.province_list
  for (let key in provinceObj) {
    if (province == provinceObj[key]) {
      areaCode += key.slice(0, 2)
    }
  }
  let cityObj: { [name: string]: any } = areaList.city_list
  for (let key in cityObj) {
    if (city == cityObj[key]) {
      areaCode += key.slice(2, 4)
    }
  }
  let countyObj: { [name: string]: any } = areaList.province_list
  for (let key in countyObj) {
    if (county == countyObj[key]) {
      areaCode += key.slice(4, 6)
    }
  }
  return areaCode

}
// console.log(areaCode);

console.log('省市区', areaList);

</script>
<style lang="scss" scoped>
.content {
  padding: 16px 16px 160px;
}
</style>